from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

# 创建 Beautiful Soup 对象
soup = BeautifulSoup(html, 'lxml')

# 打开本地 HTML 文件的方式来创建对象
#soup = BeautifulSoup(open('index.html'), 'lxml')

# 格式化输出 soup 对象的内容
format_htmltext = soup.prettify()

''' ------- 1.节点的信息获取及子孙节点 ------- '''
# 获取第一个标签对象, 以下四个都是Tag对象, Tag对象的属性: name attrs string
title_element = soup.title
head_element = soup.head
a_element = soup.a
p_element = soup.p
a_tagname = a_element.name                  # "a"
a_href = a_element.attrs['href']            # "http://example.com/elsie"
a_element.attrs['href'] = 'www.baidu.com'   # 修改, 可以直接影响到soup对象
del a_element.attrs['href']                 # 删除该属性
title_text = title_element.string           # "The Dormouse's story"

# 获取所有直接子节点, 返回列表
head_childlist = head_element.contents
title_text = head_childlist[0].string        # "The Dormouse's story"
                          
# 获取所有子孙节点, 返回迭代器
head_descendants = head_element.descendants
for element in head_descendants:
    print(element.string)



''' ------- 2.搜索文档树很实用 ------- '''
# 获取所有节点, 返回列表
a_list = soup.find_all('a')                 # 传字符串
a_list = soup.find_all('^a$')               # 传正则
a_b_list = soup.find_all(['a','b'])         # 传列表

# 获取id=link2的节点, 返回列表
id_link2_list = soup.find_all(id='link2')
# 获取文本为Elsie的节点, 返回列表
text_Elsie_list = soup.find_all(text="Elsie")



''' ------- 3.css选择器, 使用方法select() ------- '''
id_link2_list = soup.select('#link2')       # 里面直接填css表达式即可

